import Vue from 'vue';
import App from './App.vue';
import router from './router';
import ElementUI from 'element-ui';
import VueI18n from 'vue-i18n';
import { messages } from './components/common/i18n';
import 'element-ui/lib/theme-chalk/index.css'; // 默认主题
//import './assets/css/theme-green/index.css'; // 浅绿色主题
//import './assets/css/theme-green/color-green.css'; // 浅绿色主题
import './assets/css/icon.css';
import './components/common/directives';
import 'babel-polyfill';
import axios from 'axios';
Vue.config.productionTip = false;
Vue.prototype.$http = axios;
Vue.use(VueI18n);
Vue.use(ElementUI, {
    size: 'small'
});
const i18n = new VueI18n({
    locale: 'zh',
    messages
});




//权限路由
var permissionMenu = [

    {
        path: '/menu',
        component: () => import('@/views/systemset/menu/list.vue'),
        meta: { title: '系统菜单' }
    },
    {
        path: '/role',
        component: () => import('@/views/systemset/role/list.vue'),
        meta: { title: '管理员角色' },
        children:[]
    }, {
        path: '/role/edit',
        component: () => import('@/views/systemset/role/edit.vue'),
        meta: { title: '管理员角色' }
    },
    {
        path: '/user',
        component: () => import('@/views/systemset/user/list.vue'),
        meta: { title: '管理员' }
    },
	{
	    path: '/tenant',
	    component: () => import('@/views/systemset/tenant/list.vue'),
	    meta: { title: '租户管理' }
	},
	{
	    path: '/log',
	    component: () => import('@/views/systemset/log/list.vue'),
	    meta: { title: '日志监控' }
	},
    /////////////////////////////////////////////////////////////////////////////
    {
        path: '/dashboard',
        component: () => import(/* webpackChunkName: "dashboard" */ '@/components/page/Dashboard.vue'),
        meta: { title: '系统首页' }
    },
    {
        path: '/icon',
        component: () => import(/* webpackChunkName: "icon" */ '@/components/page/Icon.vue'),
        meta: { title: '自定义图标' }
    },
    {
        path: '/table',
        component: () => import(/* webpackChunkName: "table" */ '@/components/page/BaseTable.vue'),
        meta: { title: '基础表格' }
    },
    {
        path: '/tabs',
        component: () => import(/* webpackChunkName: "tabs" */ '@/components/page/Tabs.vue'),
        meta: { title: 'tab选项卡' }
    },
    {
        path: '/form',
        component: () => import(/* webpackChunkName: "form" */ '@/components/page/BaseForm.vue'),
        meta: { title: '基本表单' }
    },
    {
        // 富文本编辑器组件
        path: '/editor',
        component: () => import(/* webpackChunkName: "editor" */ '@/components/page/VueEditor.vue'),
        meta: { title: '富文本编辑器' }
    },
    {
        // markdown组件
        path: '/markdown',
        component: () => import(/* webpackChunkName: "markdown" */ '@/components/page/Markdown.vue'),
        meta: { title: 'markdown编辑器' }
    },
    {
        // 图片上传组件
        path: '/upload',
        component: () => import(/* webpackChunkName: "upload" */ '@/components/page/Upload.vue'),
        meta: { title: '文件上传' }
    },
    {
        // vue-schart组件
        path: '/charts',
        component: () => import(/* webpackChunkName: "chart" */ '@/components/page/BaseCharts.vue'),
        meta: { title: 'schart图表' }
    },
    {
        // 拖拽列表组件
        path: '/drag',
        component: () => import(/* webpackChunkName: "drag" */ '@/components/page/DragList.vue'),
        meta: { title: '拖拽列表' }
    },
    {
        // 拖拽Dialog组件
        path: '/dialog',
        component: () => import(/* webpackChunkName: "dragdialog" */ '@/components/page/DragDialog.vue'),
        meta: { title: '拖拽弹框' }
    },
    {
        // 国际化组件
        path: '/i18n',
        component: () => import(/* webpackChunkName: "i18n" */ '@/components/page/I18n.vue'),
        meta: { title: '国际化' }
    },
    {
        // 权限页面
        path: '/permission',
        component: () => import(/* webpackChunkName: "permission" */ '@/components/page/Permission.vue'),
        meta: { title: '权限测试', permission: true }
    },
    {
        path: '/404',
        component: () => import(/* webpackChunkName: "404" */ '@/components/page/404.vue'),
        meta: { title: '404' }
    },
    {
        path: '/403',
        component: () => import(/* webpackChunkName: "403" */ '@/components/page/403.vue'),
        meta: { title: '403' }
    },
    {
        path: '/donate',
        component: () => import(/* webpackChunkName: "donate" */ '@/components/page/Donate.vue'),
        meta: { title: '支持作者' }
    }
];

var constantRouterMap = [
    {
        path: '/',
        redirect: '/dashboard'
    },
    {
        path: '/',
        component: () => import(/* webpackChunkName: "home" */ '@/components/common/Home.vue'), //路由懒加载
        meta: { title: '自述文件' },
        children: permissionMenu
    },
    {
        path: '/login',
        component: () => import(/* webpackChunkName: "login" */ '@/components/page/Login.vue'),
        meta: { title: '登录' }
    },
    {
        path: '*',
        redirect: '/404'
    }
];

router.addRoutes(constantRouterMap);


//使用路由守卫对路由进行权限跳转
router.beforeEach((to, from, next) => {

    document.title = to.meta.title;
    //document.title = `${to.meta.title}|管理系统`;
    const token = localStorage.getItem('token');
	
    if (!token && to.path !== '/login') {
        next('/login');
    } else if (to.meta.permission) {
        // 如果是管理员权限则可进入，这里只是简单的模拟管理员权限而已
        token === 'admin' ? next() : next('/403');
    } else {
        // 简单的判断IE10及以下不进入富文本编辑器，该组件不兼容
        if (navigator.userAgent.indexOf('MSIE') > -1 && to.path === '/editor') {
            Vue.prototype.$alert('vue-quill-editor组件不兼容IE10及以下浏览器，请使用更高版本的浏览器查看', '浏览器不兼容通知', {
                confirmButtonText: '确定'
            });
        } else {
            next();
        }
    }
});

new Vue({
    router,
    i18n,
    render: h => h(App)
}).$mount('#app');
